package com.young.testcoroutine

import kotlinx.coroutines.*
import org.junit.Test

/**
 * Author：young
 * .
 * Time： 2022/9/13 1:05 周二
 * .
 * Desc：协程作用域
 */
class CoroutineTest04 {


    /**
     * 测试协程作用域构建器
     */
    @Test
    fun testCoroutineScopeBuilder() = runBlocking {
        // 协程作用域构建器，里面自带 CoroutineScope
        coroutineScope {
            val job1 = launch {
                delay(400)
                println("job1 完成了")
            }

            val job2 = async {
                delay(200)
                println("job2 完成了")
                "job2 返回值"
//                throw IllegalArgumentException("我是协程2抛出的异常")
            }

        }
    }

    /**
     * 测试协程作用域构建器(子协程抛出异常的状态)
     */
    @Test
    fun testCoroutineScopeBuilder1() = runBlocking {
        coroutineScope {
            val job1 = launch {
                delay(400)
                println("job1 完成了")
            }

            val job2 = async {
                delay(200)
                println("job2 完成了")
                "job2 返回值"
                throw IllegalArgumentException("我是协程2抛出的异常")
            }

        }
    }

    /**
     * 测试 supervisorScope
     */
    @Test
    fun testSupervisorScopeBuilder() = runBlocking {
        supervisorScope {
            val job1 = launch {
                delay(400)
                println("job1 完成了")
            }

            val job2 = async {
                delay(200)
                println("job2 完成了")
                "job2 返回值"
                throw IllegalArgumentException("我是协程2抛出的异常")
            }

        }
    }

}